package org.jsola.hr.helper;

import org.jsola.common.SafeKit;
import org.jsola.hr.dto.AttendShiftAddDTO;
import org.jsola.hr.dto.AttendShiftUpdateDTO;
import org.jsola.hr.dto.attendschedule.AttendShiftSaveDTO;
import org.jsola.hr.entity.AttendShiftDO;
import org.jsola.hr.query.AttendShiftQuery;
import org.jsola.hr.service.IAttendShiftService;
import org.jsola.hr.vo.AttendShiftVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class AttendShiftTestHelper extends BaseTestHelper {
    
    @Autowired
    private IAttendShiftService attendShiftService;
    @Autowired
    private AttendPeriodTestHelper attendPeriodTestHelper;

    public List<AttendShiftSaveDTO> getAttendShiftList() {

        List<AttendShiftSaveDTO> list = new ArrayList<AttendShiftSaveDTO>();
        for (int i = 0; i <3 ; i++) {
            list.add(this.getAttendShiftSaveDTO());
        }

        return list;
    }

    /**
     * 获取考勤班次
     * @return
     */
    public AttendShiftSaveDTO getAttendShiftSaveDTO() {

        AttendShiftSaveDTO dto = new AttendShiftSaveDTO();

        // 班次名称
        dto.setName(getRandomValue("varchar",50));
        // 考勤时段
        dto.setAttendPeriodList(attendPeriodTestHelper.getAttendPeriod());
        // 允许迟到早退 ; 1-允许,0-不允许
        dto.setIsAllowedLateEarly(SafeKit.getInteger(getRandomValue("tinyint",0)));
        // 上班允许迟到(分钟)
        dto.setLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
        // 班允许早退(分钟)
        dto.setEarlyMinute(SafeKit.getInteger(getRandomValue("int",0)));
        // 允许晚走晚到 ; 1-允许,0-不允许
        dto.setIsAllowLateLate(SafeKit.getInteger(getRandomValue("tinyint",0)));
        // 下班晚走(分钟)
        dto.setOffWorkLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
        // 上班晚到(分钟)
        dto.setOnWorkLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
        // 最早上班打卡时间(分钟)
        dto.setEarliestPunchTime(SafeKit.getInteger(getRandomValue("int",0)) + "");
        // 最晚下班打卡时间(分钟)
        dto.setLatestPunchTime(SafeKit.getInteger(getRandomValue("int",0)) + "");
        // 班是否需要打卡 : 1-是,0-否
        dto.setAfterWorkNeedPunch(SafeKit.getInteger(getRandomValue("tinyint",0)));

        return dto;
    }


    /**
     * 生成add测试数据
     * @return AttendShiftAddDTO
     */
    public AttendShiftAddDTO getAttendShiftAddDTO(){
        AttendShiftAddDTO attendShiftAddDTO = new AttendShiftAddDTO();
        attendShiftAddDTO.setAttendPlanId(getRandomValue("varchar",32));
        attendShiftAddDTO.setName(getRandomValue("varchar",50));
        attendShiftAddDTO.setPeriodJson(getRandomValue("varchar",1000));
        attendShiftAddDTO.setIsAdvanc(SafeKit.getBoolean(getRandomValue("tinyint",0)));
        attendShiftAddDTO.setIsAllowedLateEarly(SafeKit.getInteger(getRandomValue("tinyint",0)));
        attendShiftAddDTO.setLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
        attendShiftAddDTO.setEarlyMinute(SafeKit.getInteger(getRandomValue("int",0)));
        attendShiftAddDTO.setIsAllowLateLate(SafeKit.getInteger(getRandomValue("tinyint",0)));
        attendShiftAddDTO.setOffWorkLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
        attendShiftAddDTO.setOnWorkLateMinute(SafeKit.getInteger(getRandomValue("int",0)));
        attendShiftAddDTO.setEarliestPunchTime(getRandomValue("varchar",10));
        attendShiftAddDTO.setLatestPunchTime(getRandomValue("varchar",10));
        attendShiftAddDTO.setAfterWorkNeedPunch(SafeKit.getInteger(getRandomValue("tinyint",0)));
        return attendShiftAddDTO;
    }


    /**
     * 生成update测试数据
     * @return AttendShiftUpdateDTO
     */
    public AttendShiftUpdateDTO getAttendShiftUpdateDTO(AttendShiftVO attendShiftVO){
        return attendShiftService.selectDOById(attendShiftVO.getId(), SITE_ID).to(AttendShiftUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return AttendShiftQuery
     */
    public AttendShiftQuery getAttendShiftQuery() {
        AttendShiftQuery attendShiftQuery = new AttendShiftQuery();
        List<AttendShiftDO> attendShiftDOList = attendShiftService.list(new AttendShiftDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(attendShiftDOList)) {
            AttendShiftDO attendShiftDO = attendShiftDOList.get(0);
            attendShiftQuery = attendShiftDO.to(AttendShiftQuery.class);
        }
        return attendShiftQuery;
    }

    /**
     * 保存示例
     * @return AttendShiftVO
     */
    public AttendShiftVO saveAttendShiftExample(){
        AttendShiftAddDTO addDTO = this.getAttendShiftAddDTO();
        AttendShiftVO attendShiftVO = attendShiftService.save(addDTO,buildTokenUser());
        return attendShiftVO;
    }


}

